/*
 * KmyVector.cpp
 *
 *  Created on: 2012-9-23
 *      Author: KamiyaSoft
 */

#include "KmyVector.h"

KmyVector::KmyVector() {
	head = NULL;
	tail = NULL;
	size = 0;
}

KmyVector::~KmyVector() {
	clear();
}

int KmyVector::getSize(){
	return size;
}

void KmyVector::clear(){
	while (head!=NULL){
		ST_VectorNode* p = head;
		head = head->next;
		delete p;
	}
	tail = NULL;
    size = 0;
}

void KmyVector::addValue(void* value){
	ST_VectorNode* newnode = new ST_VectorNode();
	newnode->value = value;
	newnode->next = NULL;
	if (tail == NULL){
		head = newnode;
		newnode->pre = NULL;
	}else{
		tail->next = newnode;
		newnode->pre = tail;
	}
	tail = newnode;
	size++;
}

void KmyVector::removeValue(void* value){
	ST_VectorNode* p = head;
	while (p!=NULL){
		if (p->value == value){
			removeNode(p);
			return;
		}
        p = p->next;
	}
}

void KmyVector::removeNode(ST_VectorNode* node){
	if (node->next!=NULL){
		node->next->pre = node->pre;
	}else{
		tail = node->pre;
	}
	if (node->pre!=NULL){
		node->pre->next = node->next;
	}else{
		head = node->next;
	}
	delete node;
	size--;
}

bool KmyVector::findValue(void *value){
    ST_VectorNode* p = head;
    while (p!=NULL){
        if (p->value == value){
            return true;
        }
        p = p->next;
    }
    return false;
}

void* KmyVector::getValueByIndex(int index){
    if (index>=size){
        return NULL;
    }
    ST_VectorNode* p = head;
    while(index>0){
        p = p->next;
        index--;
    }
    return p->value;
}

ST_VectorNode* KmyVector::getHead(){
	return head;
}

ST_VectorNode* KmyVector::getTail(){
	return tail;
}
